package com.cell.controller;


import com.cell.common.Result;
import com.cell.common.States;
import com.cell.pojo.DTO.user.signUpDTO;
import com.cell.service.user.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cloudJiang
 * @since 2022-06-04
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息操作相关接口")
public class UserController {
    @Autowired
    IUserService userService;

    /**
     * 修改头像 TODO mp实现
     *
     * @return
     */
    @PostMapping("/updateIcon")
    Result updateIcon() {
        return new Result();
    }

    /**
     * 修改用户名
     *
     * @return
     */
    @ApiOperation(value = "修改用户名", notes = "修改用户名称，当传入的名称无人使用时修改，验证名称是否使用接口：/getByUserName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "要修改的名称", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @GetMapping("/updateName")
    Result updateName(@RequestParam("userName") String userName, @RequestParam("userId") Long userId) {
        userService.updateName(userId, userName);
        return new Result(null,
                States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 查看数据库中是否已经存在该用户名
     *
     * @return
     */
    @ApiOperation(value = "查询该名称", notes = "查看数据库中是否已经存在该用户名，无时data中不放置数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "要查询的名称", required = true)
    })
    @GetMapping("/getByUserName")
    Result getByUserName(@RequestParam("userName") String userName) {
        return new Result(userService.getByUserName(userName),
                States.OK.getStates(), States.OK.getDescription());
    }


    /**
     * 用户注销
     *
     * @return
     */
    @ApiOperation(value = "用户注销", notes = "在验证密码或绑定邮箱、手机后，可以执行该方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true)
    })
    @GetMapping("/deleteUser")
    Result deleteUser(@RequestParam Long id) {
        userService.deleteUser(id);
        return new Result(null
                , States.OK.getStates()
                , States.OK.getDescription());
    }

    /**
     * 用户注册
     *
     * @return
     */
    @ApiOperation(value = "用户注册", notes = "在验证用户名，手机号唯一后可以进行注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名称", required = true),
            @ApiImplicitParam(name = "phone", value = "用户号码,唯一且验证可用", required = true),
            @ApiImplicitParam(name = "password", value = "用户密码，不少于八位", required = true),
            @ApiImplicitParam(name = "name", value = "城市名称", required = true),
            @ApiImplicitParam(name = "district", value = "所在城市区域名称", required = true),
            @ApiImplicitParam(name = "district", value = "所在区域区域名称", required = true),
            @ApiImplicitParam(name = "community", value = "所在社区名称", required = true),
            @ApiImplicitParam(name = "locationDetails", value = "详细地址，XX栋XX楼XX号", required = true),
            @ApiImplicitParam(name = "signUpDTO", value = "以上属性封装到改DTO")
    })
    @PostMapping("/signUp")
    Result signUp(@Validated @RequestBody signUpDTO signUpDTO) {
        userService.signUp(signUpDTO);
        return new Result(null, States.OK.getStates()
                , States.OK.getDescription());
    }

    /**
     * @return
     */
    @ApiOperation(value = "用户查看当前社区代理", notes = "当前社区无代理时，抛出异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "locationId", value = "当前用户的locationId", required = true),
    })
    @GetMapping("/getCommunityAgent")
    Result getCommunityAgent(@RequestParam Long locationId) {
        return new Result(userService.getCommunityAgent(locationId), States.OK.getStates()
                , States.OK.getDescription()
        );
    }

    /**
     * 用户查看附近社区代理
     *
     * @return
     */
    @ApiOperation(value = "用户查看附近社区代理", notes = "附近社区无代理时，抛出异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "locationId", value = "当前用户的locationId", required = true),
    })
    @GetMapping("/getNearbyCommunityAgent")
    Result getNearbyCommunityAgent(@RequestParam Long locationId) {
        return new Result(userService.getNearbyCommunityAgent(locationId),
                States.OK.getStates()
                , States.OK.getDescription());
    }

    /**
     * 用户查看当前区域代理
     *
     * @return
     */
    @ApiOperation(value = "用户查看该区域的代理", notes = "当前区域无代理时，抛出异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "locationId", value = "当前用户的locationId", required = true),
    })
    @GetMapping("/getNearbyDistrictAgent")
    Result getNearbyDistrictAgent(@RequestParam Long locationId) {
        return new Result(userService.getNearbyDistrictAgent(locationId),
                States.OK.getStates()
                , States.OK.getDescription());
    }


    /**
     * 获取用户基本信息
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户基本信息", notes = "获取相关用户的基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "Long", required = true)
    })
    @GetMapping("/getUserMessage")
    Result getUserMessage(@RequestParam Long userId) {
        return new Result(userService.getUserMessage(userId), States.OK.getStates()
                , States.OK.getDescription());
    }
}

